package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.DateUtilPlus;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.BOrderVitalEntity;
import io.lkt.modules.sys.entity.PageDetailLogEntity;
import io.lkt.modules.sys.entity.PushHeadLogEntity;
import io.lkt.modules.sys.service.PageDetailLogService;
import io.lkt.modules.sys.service.PushHeadLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys/pagedetaillog")
public class PageDetailLogController extends AbstractController {
    @Autowired
    private PageDetailLogService pageDetailLogService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pageDetailLogService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/queryList")
    public R queryList(@RequestParam Map<String, Object> params) throws ParseException {
        if (params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.ok();
        }

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {

            String startDate = params.get("startDate").toString();
            params.put("startDate", startDate); //今天开始时间
            params.put("endDate", startDate + " 23:59:59"); //今天结束时间

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(startDate);

            String yesterday = DateUtilPlus.formatDate(DateUtilPlus.getYesterday(date));
            params.put("startYesterDay", yesterday); //昨天开始时间
            params.put("endYesterDay", yesterday + " 23:59:59"); //昨天结束时间

            String lastWeekToDay = DateUtilPlus.formatDate(DateUtilPlus.getToDayOfLastWeek(date));
            params.put("startLastWeekToDay", lastWeekToDay); //上周同期开始时间
            params.put("endLastWeekToDay", lastWeekToDay + " 23:59:59"); //上周同期结束时间

            String lastWeekFirstDay = DateUtilPlus.formatDate(DateUtilPlus.getFirstDayOfLastWeek(date));
            params.put("startLastWeekFirstDay", lastWeekFirstDay); //上周开始时间
            String lastWeekLastDay = DateUtilPlus.formatDate(DateUtilPlus.getLastDayOfLastWeek(date));
            params.put("endLastWeekLastDay", lastWeekLastDay + " 23:59:59"); //上周结束时间

            String lastMonthFirstDay = DateUtilPlus.formatDate(DateUtilPlus.getFirstDayOfLastMonth(date));
            params.put("startLastMonthFirstDay", lastMonthFirstDay); //上月开始时间
            String lastMonthLastDay = DateUtilPlus.formatDate(DateUtilPlus.getLastDayOfLastMonth(date));
            params.put("endLastMonthLastDay", lastMonthLastDay + " 23:59:59"); //上月结束时间

        }

        Query query = new Query(params);
        List<PageDetailLogEntity> list = pageDetailLogService.queryList(query);
        int total = pageDetailLogService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/queryMainPage")
    public R queryMainPage(@RequestBody Map<String, Object> params) {

        String startDate = DateUtil.getFormatDate("yyyy-MM-dd", new Date());
        String endDate = DateUtil.getFormatDate("yyyy-MM-dd", new Date()) + " 23:59:59";

        if (params.get("pDate") != null) {
            startDate = params.get("pDate").toString();
            endDate = params.get("pDate").toString() + " 23:59:59";
        }
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        //Query query = new Query(params);

        Map<String, Object> rMap=new HashMap<>();

        List<PageDetailLogEntity> list = pageDetailLogService.queryMainList(params);
        if (list != null && list.size() > 0) {
            for (PageDetailLogEntity pdl : list) {
                if (pdl.getPageName().equals("文章库")) {
                    rMap.put("wzk",pdl.getCout());
                } else if (pdl.getPageName().equals("早晚安打卡")) {
                    rMap.put("zwa",pdl.getCout());
                } else if (pdl.getPageName().equals("人脉广场")) {
                    rMap.put("rmgc",pdl.getCout());
                } else if (pdl.getPageName().equals("客户")) {
                    rMap.put("kh",pdl.getCout());
                } else if (pdl.getPageName().equals("订单支付页")) {
                    rMap.put("zfy",pdl.getCout());
                }
            }
        }

        return R.ok().put("vital", rMap);
    }
}
